# -*- coding:utf-8 -*-
"""
Author：Administrator
Date:2021年10月14日
"""
import pandas as pd

path = './数据/附加阻尼比计算原始数据.xlsx'

data_link = pd.read_excel(path, sheet_name='连接单元数据', skiprows=2, usecols=[0, 1, 4, 6, 8], names=['连接单元号', '时程', 'DX',
                                                                                                 'DY', 'DZ'])

# 筛选只包含'(全部)'的数据行
con = data_link['时程'].str.contains('全部')
data_link = data_link[con].reset_index(drop=True)

data_lx = pd.read_excel(path, sheet_name='X向阻尼器编号')
data_ly = pd.read_excel(path, sheet_name='Y向阻尼器编号')

table_name = list(data_link['时程'].drop_duplicates())


def Force(data):
	d = data['阻尼器变形(mm)']
	F = data['屈服力/摩擦荷载(kN)']
	k = data['初始刚度(kN/mm)']
	Rotio = data['第二刚度比(kN/mm)']
	dy = data['屈服/起滑位移(mm)']
	if d >= dy:
		Force = F + Rotio * k * (d - dy)
	else:
		Force = d * k
	return Force

def Wci(data):
	d = data['阻尼器变形(mm)']
	F = data['屈服力/摩擦荷载(kN)']
	k = data['初始刚度(kN/mm)']
	Rotio = data['第二刚度比(kN/mm)']
	dy = data['屈服/起滑位移(mm)']
	Fu = data['阻尼器出力(kN)']
	if d >= dy:
		Wci = 4*(F*d-Fu*dy)
	else:
		Wci = 0

	return Wci

new_data = pd.ExcelWriter('./数据/08结果-连接单元拆分.xlsx')

for i in table_name:
	data2 = data_link[data_link['时程'] == i]
	if 'X' in str(i):
		data2 = pd.merge(data2, data_lx, on=['连接单元号'])
	else:
		data2 = pd.merge(data2, data_ly, on=['连接单元号'])

	data2.loc[data2['连接单元方向'] == 1, '阻尼器变形(mm)'] = data2['DX']
	data2.loc[data2['连接单元方向'] == 2, '阻尼器变形(mm)'] = data2['DY']
	data2.loc[data2['连接单元方向'] == 3, '阻尼器变形(mm)'] = data2['DZ']
	data2['阻尼器变形(mm)'] = abs(data2['阻尼器变形(mm)'])
	data2['初始刚度(kN/mm)'] = data2['屈服力/摩擦荷载(kN)'] / data2['屈服/起滑位移(mm)']
	data2['阻尼器出力(kN)'] = data2.apply(Force, axis=1)
	data2['Wci(kN·mm)'] = data2.apply(Wci, axis=1)
	data2 = data2.drop(labels=['时程', 'DX', 'DY', 'DZ', '连接单元方向'], axis=1)

	temp = data2.连接单元号
	data2 = data2.drop('连接单元号', axis=1)
	data2.insert(1, '连接单元号', temp)

	temp = data2['初始刚度(kN/mm)']
	data2 = data2.drop('初始刚度(kN/mm)', axis=1)
	data2.insert(4, '初始刚度(kN/mm)', temp)

	data2 = data2.copy()
	data2['Wci(kN·mm)'] = data2['Wci(kN·mm)'].astype('float64')
	data2.loc['合计'] = data2[['Wci(kN·mm)']].agg('sum')

	data2.to_excel(new_data, sheet_name=i.replace('全部', 'link'), index=False)

new_data.save()
new_data.close()
print('输出成功!')